package br.edu.utfpr.md.tads;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import br.edu.utfpr.md.tads.controller.DialogConnection;
import br.edu.utfpr.md.tads.model.Contato;
import br.edu.utfpr.md.tads.view.dialog.DialogPostContato;
import br.edu.utfpr.md.tads.view.dialog.ViewContatoDialog;

public class MainActivity extends Activity implements OnItemClickListener, OnClickListener{
	private EditText editTextBuscar;
	private ImageButton imageButtonBuscar;
	private ListView listViewContatos;
	private DialogConnection connection;
	private Contato contato;
	private Button buttonNovo;
	
	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_filter);
		if (android.os.Build.VERSION.SDK_INT > 9) {
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);
		}
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		editTextBuscar = (EditText) findViewById(R.screen_filter.edit_text_busca);
		imageButtonBuscar = (ImageButton) findViewById(R.screen_filter.image_button_buscar);
		imageButtonBuscar.setOnClickListener(this);
		listViewContatos = (ListView) findViewById(R.screen_filter.list_view_contatos);
		listViewContatos.setOnItemClickListener(this);
		listViewContatos.setOnCreateContextMenuListener(this);		
		
		buttonNovo = (Button) findViewById(R.screen_filter.button_new);
		buttonNovo.setOnClickListener(this);
		carregar();
	}

	
	private void carregar() {
		connection = new DialogConnection(this, listViewContatos);
		connection.execute();
	}


	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
		contato = (Contato) adapter.getItemAtPosition(position);
		ViewContatoDialog contatoDialog = new ViewContatoDialog(this, contato,listViewContatos);
		contatoDialog.show();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.screen_filter.image_button_buscar:
			String busca = editTextBuscar.getText().toString().trim();
			if (busca.length() > 0 ) {
				connection = new DialogConnection(this, listViewContatos);
				connection.execute(busca);
			}			
			break;
		case R.screen_filter.button_new:
			DialogPostContato postContato = new DialogPostContato(this, new Contato(),listViewContatos);
			postContato.show();
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.activity_main, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.menu.menu_novo:
			DialogPostContato postContato = new DialogPostContato(this, new Contato(),listViewContatos);
			postContato.show();
			break;
		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}
}
